using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Configuration;
using System.Data.SqlClient;

public partial class myConfiguration : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (null != Membership.GetUser())
        {
            if (Roles.IsUserInRole(Membership.GetUser(true).UserName, "admin"))
            {
                SmtpSection smtpSec = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
                ((Label)this.LoginViewAdmin.FindControl("NetworkHost")).Text = smtpSec.Network.Host;
                ((Label)this.LoginViewAdmin.FindControl("NetworkUserName")).Text = smtpSec.Network.UserName;
                /*((Label)this.LoginViewAdmin.FindControl("NetworkPassword")).Text = smtpSec.Network.Password;*/
                if (smtpSec.Network.Password != "")
                    ((Label)this.LoginViewAdmin.FindControl("NetworkPassword")).Text = "Yes";
                ((Label)this.LoginViewAdmin.FindControl("NetworkPort")).Text = smtpSec.Network.Port.ToString();
                ((Label)this.LoginViewAdmin.FindControl("AdminEmail")).Text = smtpSec.From;

                if (!IsPostBack)
                {
                    SqlConnection connection = 
                        new SqlConnection(ConfigurationManager.ConnectionStrings["sylibSettings"].ConnectionString);
                    connection.Open();
                    string sCommand = "SELECT [sendAdminEmail] FROM [settings] WHERE [id] = 0";
                    SqlCommand cmd = new SqlCommand(sCommand, connection);
                    int sendAdminEmail = (int)cmd.ExecuteScalar();
                    if (0 == sendAdminEmail)
                        ((CheckBox)this.LoginViewAdmin.FindControl("ConfirmCheck")).Checked = false;
                    else
                        ((CheckBox)this.LoginViewAdmin.FindControl("ConfirmCheck")).Checked = true;
                    connection.Close();
                }
            }
        }
    }


    protected void updateButton_Click(object sender, EventArgs e)
    {
        SqlConnection connection = 
            new SqlConnection(ConfigurationManager.ConnectionStrings["sylibSettings"].ConnectionString);
        connection.Open();
        string sCommand;
        if (((CheckBox)this.LoginViewAdmin.FindControl("ConfirmCheck")).Checked)
            sCommand = "UPDATE [settings] SET [sendAdminEmail] = 1 WHERE [id] = 0";
        else
            sCommand = "UPDATE [settings] SET [sendAdminEmail] = 0 WHERE [id] = 0";
        SqlCommand cmd = new SqlCommand(sCommand, connection);
        cmd.ExecuteNonQuery();
        connection.Close();
    }
}